/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gov.mtc.dgac.licencias.persistence;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import pe.gov.mtc.dgac.licencias.bean.AuditorTipoauditTO;
import pe.gov.mtc.dgac.licencias.bean.AuditoriaTO;
import pe.gov.mtc.dgac.licencias.bean.TipoAuditoriaTO;
import pe.gov.mtc.dgac.licencias.util.Utiles;

/**
 *
 * @author elvis
 */
@Repository
public class AuditoriaHibDAO extends HibernateDaoSupport implements AuditoriaDAO {

    private final static Logger logger = Logger.getLogger(AuditoriaHibDAO.class);
    
    @Autowired
    public AuditoriaHibDAO(SessionFactory sessionFactory){
         setHibernateTemplate( new HibernateTemplate(sessionFactory));
    }
    
    public List<AuditoriaTO> listarAuditorias(AuditoriaTO auditoria) {
        DetachedCriteria criteria = DetachedCriteria.forClass(AuditoriaTO.class);
        logger.debug("listarAuditorias()");
        if(auditoria !=null){
            if( !Utiles.nullToBlank( auditoria.getCodAuditoria()+"" ).equals("")){
                 criteria.add( Restrictions.eq("codAuditoria", auditoria.getCodAuditoria()) );
            }
        }				
        return getHibernateTemplate().findByCriteria(criteria);
    }

    public void registrarAuditoria(AuditoriaTO auditoria) {
        logger.debug("registrarAuditoria ");
        this.getHibernateTemplate().saveOrUpdate(auditoria);
    }

    public void eliminarauditoria(Integer auditoria) {
         getSession().createQuery(" delete from  AuditoriaTO where codAuditoria = :id ")
        .setInteger("id", auditoria)
        .executeUpdate();
    }

    public AuditoriaTO obtenerAuditoria(Integer auditoria) {
        Query query = getSession().createQuery(" from AuditoriaTO a where codAuditoria = :id ")
        .setInteger("id", auditoria);
        return (AuditoriaTO) query.uniqueResult();
    }

    public List<TipoAuditoriaTO> listatipoAuditoria() {
        return this.getHibernateTemplate().find("from TipoAuditoriaTO th ");
    }

    public List<AuditorTipoauditTO> listarAuditoresAptos() {
        Query query = getSession().createQuery(" from AuditorTipoauditTO a where estado = 1 ");
        return query.list();
    }

}
